home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 4,401 to 4,500 / aol-file-protocol-4400-4401-to-4500.zip / AOLDLs / PDA-Newton Development / ND+ Creating a Preferences en / prefs.sit / Prefs.txt next >
Text File  |  1994-12-31  |  4KB  |  81 lines

  1. Creating a Preferences entry
  2.  
  3. This development tip is formatted in both Microsoft Word 5.1 and plain text. In Word, all normal text is in the New York font. and all code is in courier. 
  4.  
  5. When your application is run, some data (such as viewBounds) can be stored in the base view, and some (such as names) can be stored in a soup entry in your application's soup, but how about preferences, like font size and preferred delay? For this type of data, one should use a preference entry. This document will discuss creating a function for creating and accessing such an entry.
  6.  
  7. The normal storage for preferences is in the System soup, located on the internal store. All applications that wish to create preference entries should use their appSymbol (as set in the "Settings..." dialog accessed from the "Project" menu) as their tag. The tag is simply a string, usually defined in the Project Data, using a DefConst(...):
  8.  
  9.     DefConst(╘kAppName, ╥Benz Cool App:BenG╙);
  10.  
  11. Now, wherever you need to reference your application╒s name, you can just use kAppName.
  12.  
  13. A typical preference entry might look like this:
  14.  
  15.     local blankPrefEntry := 
  16.         {
  17.         tag:         kAppName,
  18.         someData:    "Boy, this is a cool entry"
  19.         };
  20.     
  21. The tag is used to access the information from the System soup, which is indexed on the tag slot. In addition, some applications may in the future use the tag slot to determine an entry's owner, so you should probably use you're appSymbol, and not something like "Benz Cool App Prefs".
  22.  
  23. To create an entry, you need a reference to the system soup. This is done by using the magic pointer ROM_systemSoupName,  which, in US Newtons is ╥System╙. Since the system soup should be only on the internal store, we don╒t need a union soup:
  24.  
  25.     local sysSoup := GetStores()[0]:GetSoup(ROM_systemSoupName);
  26.  
  27. Now, make your entry and add it to the soup:
  28.  
  29.     sysSoup:Add(blankPrefEntry);
  30.  
  31. To access your entry, first create a cursor in the system soup like this:
  32.     
  33.     local searchSpec := {type: 'index, indexPath: 'tag};
  34.     local systemCursor := Query(sysSoup , searchSpec);
  35.     
  36. Now that you╒ve got a cursor, you need to move it to your entry:
  37.  
  38.     local prefEntry := systemCursor:GotoKey(kAppName);
  39.     
  40. Because of the way that the :GotoKey(...) method works, if there is no entry in the system soup with the appropriate name, it will move the cursor to the closest tag. This means that we have to make sure we╒ve got the right entry by doing a StrEqual(...) on both the entry╒s tag and the application name. If they╒re not equal, then our entry doesn╒t exist:
  41.  
  42.     IF prefEntry AND StrEqual(prefEntry.tag, kAppName) THEN
  43.         return prefEntry;
  44.     
  45. Now that we can create and access a preferences entry, it makes sense to encapsulate all of this into one function that can be called from anywhere in our app. This is done by DefConst(...)╒ing a function in the project data, which we will call when we want the prefs:
  46.  
  47.     DefConst(╘kGetPrefsFunc,
  48.     func() begin
  49.         local internal := GetStores()[0];
  50.         local sysSoup := internal:GetSoup(ROM_systemSoupName);
  51.  
  52.         local searchSpec := {type: 'index, indexPath: 'tag};
  53.         local systemCursor := Query(sysSoup , searchSpec);
  54.  
  55.         local prefEntry := systemCursor:GotoKey(kAppName);
  56.     
  57.         IF prefEntry AND StrEqual(prefEntry.tag, kAppName) THEN
  58.             return prefEntry;
  59.  
  60.         prefEntry := 
  61.                 {
  62.                 tag:     kAppName,
  63.                 someData:    "Boy, this is a cool entry"
  64.                 };
  65.         sysSoup:Add(prefEntry);
  66.  
  67.         return prefEntry;
  68.     end);
  69.     
  70. Now, whenever we need prefs in our app, we can get them by using the line:
  71.  
  72.     local prefs := CALL kGetPrefsFunc WITH ();
  73.  
  74.     This will call our function and return a prefs entry, whether one exists or needs to be created. This function can be pasted directly into your project data, just make sure to change the blank pref entry to suit your purposes.
  75.  
  76. If you have any questions or comments about this document, or requests for future similar documents, please feel free to email me at America Online, gottlb.
  77.  
  78. Ben Gottlieb (gottlb)
  79. PDA Forum Consulant
  80. Keyword PDA is how to find us!
  81.